package com.kuang.annotation;

import javax.print.PrintService;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test10 {
    //普通方法调用
    public static void test01(){
        Person p1 = new Person();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            p1.getName();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通方式跑十亿次:"+(endTime-startTime)+"ms");
    }
    //反射方式调用
    public static void test02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Person p1 = new Person();
        Class c1 = p1.getClass();
        Method getName = c1.getDeclaredMethod("getName", null);
//        Class c1 = Class.forName("com.kuang.annotation.Person");
//        Person p1 = (Person)c1.newInstance();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            getName.invoke(p1,null);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("反射获取对象跑十亿次:"+(endTime-startTime)+"ms");
    }
    //反射方式调用，，权限检测关闭

    public static void test03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Person p1 = new Person();
        Class c1 = p1.getClass();
        Method getName = c1.getDeclaredMethod("getName", null);
        getName.setAccessible(true);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            getName.invoke(p1,null);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("关闭检测跑十亿次:"+(endTime-startTime)+"ms");
    }

    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
        test01();
        test02();
        test03();
    }
}
